﻿
//Author: 张亚涛
//createtime:2015-12-15 23:08:47
//Description:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using NS.DAL.Systems;
using Model;
using DAL;

namespace DAL
{
    /// <summary>
    /// 该类提供了一系列操作 "Info" 表的方法;
    /// </summary>
    public partial class InfoBase
    {
        const string SQL_Select = "select [ID],[Title],[Contents],[Remarks] from [Info] ";
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 返回Info表的所有数据 
        /// </summary>
        /// <returns></returns>        
        public static List<Info> GetAllInfo()
        {
            List<Info> infoList = new List<Info>();
            SqlDataReader reader = null;

            string sql = SQL_Select;
            reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                Info info = new Info(reader);
                infoList.Add(info);
            }
            reader.Close();
            return infoList;
        }
        /// <summary>
        /// 根据Info表的ID字段返回数据 
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>       
        public static Info GetInfoById(int id)
        {
            Info info = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                info = new Info(reader);
            }
            reader.Close();
            return info;
        }
        /// <summary>
        /// 根据Info表的Title字段返回数据 
        /// </summary>
        /// <param name="Title">标题</param>
        /// <returns></returns>       
        public static Info GetInfoByTitle(string Title)
        {
            Info info = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [Title] = @Title";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Title",Title)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                info = new Info(reader);
            }
            reader.Close();
            return info;
        }
        #endregion
        #region delete data
        /// <summary>
        /// 根据Info表的ID字段删除数据 
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>        
        public static int DeleteInfoById(int id)
        {
            string sql = "delete from [Info] where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region update data
        /// <summary>
        /// prepare parameters 
        /// </summary>
        public static SqlParameter[] PrepareCommandParameters(Info info)
        {
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",info.Id),
            new SqlParameter("@Title",info.Title),
            new SqlParameter("@Contents",info.Contents),
            new SqlParameter("@Remarks",info.Remarks)    
            };
            return paras;
        }
        /// <summary>
        /// 根据Info表的ID字段更新数据 
        /// </summary> 
        /// <param name="Info">info</param>
        /// <returns></returns>       
        public static int UpdateInfoById(Info info)
        {
            string sql = "update [Info] set [Title] = @Title,[Contents] = @Contents,[Remarks] = @Remarks where [ID] = @Id";
            SqlParameter[] paras = PrepareCommandParameters(info);
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region insert data
        /// <summary>
        /// 向Info表插入一条数据，返回自增列数值，插入不成功则返回-1
        /// </summary>
        /// <param name="info">Info</param>       
        /// <returns></returns>        
        public static int AddInfo(Info info)
        {
            string sql = "insert into [Info]([Title],[Contents],[Remarks])values(@Title,@Contents,@Remarks)    select @@identity";
            SqlParameter[] paras = PrepareCommandParameters(info);
            int id = -1;

            string _ID = SQLHelper.ExecuteScalar(sql, paras).ToString();
            int.TryParse(_ID, out id);

            return id;
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 返回Info表的所有数据 （SQL语句）
        /// </summary>
        /// <returns></returns>        
        public static string GetAllInfo_SQL()
        {
            return SQL_Select;
        }
        #endregion
    }
}